package MyApp.View.report
{
	import MyApp.Model.proxy.ReportProxy;
	
	import components.report.Report2;
	import components.report.ReportContext;
	import components.report.ReportWindow;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.charts.series.LineSeries;
	import mx.collections.ArrayCollection;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import util.Event.ValueEvent;
	
	public class ReportMediator extends Mediator
	{
		public static const NAME:String="ReportMediator";
		public function ReportMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			view.addEventListener(ReportWindow.DO_QUERY_REPORT1,doQueryReport1);
			view.addEventListener(ReportWindow.DO_QUERY_REPORT2,doQueryReport2);
			view.addEventListener(ReportWindow.REPORT_CHANGE,reportChange);
		}
		private function get view():ReportWindow{
			return viewComponent as ReportWindow;
		}
		protected function reportChange(e:ValueEvent):void{
			switch(e.vo){
				case 0:
					this.refreshReport1();
					break;
				case 1:
					this.refreshReport2();
					break;
			}
		}
		protected function doQueryReport1(e:MouseEvent):void{
			this.refreshReport1();
		}
		protected function doQueryReport2(e:MouseEvent):void{
			this.refreshReport2();
		}
		protected function refreshReport1():void{
			var groupId:int=view.report1.gList.selectedItem.wellGroupId;
			var year:int=view.report1.dList.selectedItem.value as int;
			var timeType:int=view.report1.tList.selectedItem.value as int;
			this.sendNotification(ReportWindow.DO_REFRESH_REPORT1,{groupId:groupId,year:year,timeType:timeType});
		}
		protected function refreshReport2():void{
			var productType:int=view.report2.tpList.selectedItem.value as int;
			var year:int=view.report2.dList.selectedItem.value as int;
			var timeType:int=view.report2.tList.selectedItem.value as int;
			this.sendNotification(ReportWindow.DO_REFRESH_REPORT2,{productType:productType,year:year,timeType:timeType});
		}
		protected function renderReport2(list:ArrayCollection):void{
			//数据表
			var columnObj:Object=list[0];
			var columns:Array=view.report2.record_data_list.arrColumns;
			columns.length=0;
			var cl:DataGridColumn;
			cl=new DataGridColumn();
			cl.headerText="日期";
			cl.dataField="date";
			cl.labelFunction=view.report2.labelFunction;
			columns.push(cl);
			for(var name:String in columnObj){
				if(name!="date"){
					cl=new DataGridColumn();
					cl.headerText=columnObj[name][0];
					cl.dataField=name;
					cl.labelFunction=view.report2.labelFunction;
					columns.push(cl);
				}
			}
			view.report2.record_data_list.dataGrid.columns=columns;
			view.report2.dataList=list;
			//图表
			var lines:Array=new Array();
			var l:LineSeries;
			var llist:ArrayCollection=new ArrayCollection();
			var lobj:Object;
			var tmp:Object;
			for(var i in list){
				lobj={};
				tmp=list.getItemAt(i);
				lobj["date"]=tmp.date;
				for(var name:String in tmp){
					if(name!="date"){
						lobj[name]=tmp[name][1];
					}
				}
				llist.addItem(lobj);
			}
			for(var name:String in columnObj){
				if(name!="date"){
					l=new LineSeries();
					l.yField=name;
					l.setStyle("form","curve");
					l.displayName=columnObj[name][0];
					lines.push(l);
				}
			}
			if(view.report2.linechart){
				view.report2.linechart.series=lines;
				view.report2.dataLList=llist;
			}else{
				var hd:Function=function (e:MouseEvent):void{
					view.report2.linechart.series=lines;
					view.report2.dataLList=llist;
					view.report2.removeEventListener(FlexEvent.CREATION_COMPLETE,hd);
				};
				view.report2.addEventListener(Report2.LINE_CREATED,hd);
			}
		}
		override public function listNotificationInterests():Array{
			return [ReportProxy.GROUP_LOAD_OK,ReportProxy.REPORT1_LOAD_OK,ReportProxy.REPORT2_LOAD_OK];
		}
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case ReportProxy.GROUP_LOAD_OK:
					view.report1.groupList=notification.getBody() as ArrayCollection;
					view.report1.gList.selectedIndex=0;
					this.refreshReport1();
					break;
				case ReportProxy.REPORT1_LOAD_OK:
					view.report1.dataList=notification.getBody() as ArrayCollection;
					break;
				case ReportProxy.REPORT2_LOAD_OK:
					renderReport2(notification.getBody() as ArrayCollection);
					break;
			}
		}
	}
}